import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_project/tool/theme/theme_data_config.dart';
import 'package:flutter_project/tool/utils/app_manager.dart';
import 'package:flutter_project/tool/utils/dz_logger.dart';
import 'package:get/get_utils/src/platform/platform.dart';
import 'package:get/route_manager.dart';

enum AppThemeMode { system, dark, light }

class AppTheme {
  AppThemeMode _themeMode = AppThemeMode.system;

  final Map<AppThemeMode, ThemeData> _themeDataMap =
      <AppThemeMode, ThemeData>{};

  AppThemeMode mode() {
    return _themeMode;
  }
  ///配置主题数据
  void config(ThemeData light, ThemeData dark) {
    _themeDataMap[AppThemeMode.light] = light;
    _themeDataMap[AppThemeMode.dark] = dark;
  }

  ThemeData? themeData() {
    return _themeDataMap[_themeMode];
  }
  ///设置主题模式
  void set(AppThemeMode themeMode, {bool isNoti = false}) {
    _themeMode = themeMode;
    if (isNoti) {
      if (themeMode == AppThemeMode.system) {
        final isDark =
            SchedulerBinding.instance.platformDispatcher.platformBrightness ==
                Brightness.dark;
        DZLog.d("set $themeMode isDark:$isDark");
        _updateTheme(null);
      } else {
        _updateTheme(themeMode == AppThemeMode.dark);
      }
    }
  }
  ///更新主题
  static void _updateTheme(bool? isDark) {
    Future.delayed(const Duration(milliseconds: 80), () {
      Get.forceAppUpdate();
      if (isDark != null) {
        if (isDark) {
          if (GetPlatform.isAndroid) {
            //指定全局页面的状态栏，导航栏等设置
            SystemChrome.setSystemUIOverlayStyle(
                ThemeConfig.systemUiOverlayStyleDarkTheme);
          }
        } else {
          if (GetPlatform.isAndroid) {
            //指定全局页面的状态栏，导航栏等设置
            SystemChrome.setSystemUIOverlayStyle(
                ThemeConfig.systemUiOverlayStyleLightThemeBlack);
          }
        }
      }
    });
  }
  ///适配主题
  static T adapt<T>(T light, T dark) {
    bool isDarkMode = Get.isDarkMode;
    if (AppManager().appTheme.mode() == AppThemeMode.light) {
      isDarkMode = false;
    } else if (AppManager().appTheme.mode() == AppThemeMode.dark) {
      isDarkMode = true;
    } else {
      final brightness =
          SchedulerBinding.instance.platformDispatcher.platformBrightness;
      if (brightness == Brightness.dark) {
        isDarkMode = true;
      } else {
        isDarkMode = false;
      }
    }

    if (Get.context == null) {
      DZLog.d(
          "Get.context == null isDarkMode:$isDarkMode mode:${AppManager().appTheme.mode()}");
    } else {
      DZLog.d(
          "Get.context != null isDarkMode:$isDarkMode mode:${AppManager().appTheme.mode()}");
    }
    return isDarkMode ? dark : light;
  }
  ///获取当前主题样式
  ThemeMode themeMode() {
    switch (mode()) {
      case AppThemeMode.dark:
        return ThemeMode.dark;
      case AppThemeMode.light:
        return ThemeMode.light;
      case AppThemeMode.system:
        final brightness =
            SchedulerBinding.instance.platformDispatcher.platformBrightness;
        if (brightness == Brightness.dark) {
          DZLog.d("Get.context == null Brightness.dark");
        } else {
          DZLog.d("Get.context == null Brightness.light");
        }
        return ThemeMode.system;
    }
  }
  ///切换到浅色模式
  static void changeThemeLight() {
    AppManager().appTheme.set(AppThemeMode.light);
    var theme = AppManager().appTheme.themeData();
    var mode = ThemeMode.light;
    theme ??= ThemeConfig.lightTheme;
    Get.changeThemeMode(mode);
    Get.changeTheme(theme);
    _updateTheme(false);
  }
  ///切换到深色模式
  static void changeThemeDark() {
    AppManager().appTheme.set(AppThemeMode.dark);
    var theme = AppManager().appTheme.themeData();
    var mode = ThemeMode.dark;
    if (theme == null) {
      mode = ThemeMode.light;
    }
    theme ??= ThemeConfig.lightTheme;
    Get.changeThemeMode(mode);
    Get.changeTheme(theme);
    _updateTheme(true);
  }
}
